<script setup lang="ts">
import axios from 'axios'
import { onMounted, ref } from 'vue'
// 频道对象
type ChannelItem = {
  id: number
  name: string
}

// 频道接口响应数据
type ChannelResData = {
  data: {
    channels: ChannelItem[]
  }
  message: string
}

const list = ref<ChannelItem[]>([])

onMounted(async () => {
  const res = await axios.get<ChannelResData>('http://geek.itheima.net/v1_0/channels')
  // console.log(res)
  list.value = res.data.data.channels
})
//导入pinia
import { useChannelStore } from '@/stores/channel'
const store = useChannelStore()

//接收传过来数据
// const props = defineProps<{
//   channelID: number
// }>()

//定义方法传回父组件

// defineEmits<{
//   (e: 'changechannelID', id: number): void
// }>()
</script>

<template>
  <div class="channel-nav">
    <nav class="list">
      <a
        class="item"
        :class="{ active: item.id === store.channelID }"
        href="javascript:;"
        v-for="(item, i) in list"
        :key="item.id"
        @click="store.changeID(item.id)"
      >
        {{ item.name }}
      </a>
    </nav>
  </div>
</template>
